#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/22 0022 11:28
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : logs

import re
import requests
import re
from io import BytesIO, StringIO
from aip import AipOcr
from PIL import Image
import pytesseract

def run():
    head = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Host': 'accounts.douban.com',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1',
    }
    bod = requests.get("https://accounts.douban.com/login", headers=head)
    imgs_url = re.search('id="captcha_image" src="(.*?)" alt="captcha" class="captcha_image"', bod.text).group(1)
    img = requests.get(imgs_url)
    print(img.url)
    print(get_words_by_img(img.content))


def get_words_by_img(img):
    APP_ID = '10703069'
    API_KEY = 'LHAPjhbGzGiyo3ooDEsC6LMx'
    SECRET_KEY = 'uidx7NiinXNxpu5yx7AgC1XtGNfr3qG6 '
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    return client.basicGeneral(img)


# import logging
# # ↓↓↓↓↓↓↓↓
# mongodb = "http://www.maiziedu.com/course/395/"
# asyncio = "http://python.jobbole.com/87310/"
# # ↑↑↑↑↑↑↑↑
# log = logging.getLogger()

def run2():
    # pytesseract.pytesseract.tesseract_cmd = "C:/Program Files (x86)/Tesseract-OCR/tesseract.exe"
    img = Image.open('1.jpg')
    code = pytesseract.image_to_string(img)
    print(code)


if __name__ == '__main__':
    run2()
